package com.lenovo.manufacture.Fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.lenovo.manufacture.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.POST;

/**
 * A simple {@link Fragment} subclass.
 */
public class AerFragment extends Fragment {
    @BindView(R.id.linT)
    TextView linT;
    @BindView(R.id.nfT)
    TextView nfT;
    @BindView(R.id.lin)
    Button lin;
    @BindView(R.id.nf)
    Button nf;
    @BindView(R.id.gj)
    Button gj;
    private View view;

    public AerFragment() {
        // Required empty public constructor
    }

    @OnClick({R.id.lin, R.id.nf, R.id.gj})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lin:
                initData(1);
                break;
            case R.id.nf:
                initData(2);
                break;
            case R.id.gj:
                initData(0);
                break;
        }
    }

    private interface BlogServer {
        @POST("updateAcSwitch")
        Call<ResponseBody> getBlog(@Body RequestBody str);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_aer, container, false);
        ButterKnife.bind(this,view);
        return view;
    }
    private void initData(int i) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/Interface/UserFactory/")
                .build();
        BlogServer blogServer = retrofit.create(BlogServer.class);
        Map<String, Integer> map = new HashMap<>();
        map.put("acSwitch", i);
        map.put("id", i);
        String s = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json:charset=utf-8"), s);
        Call<ResponseBody> blog = blogServer.getBlog(requestBody);
        blog.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                linT.setTextColor(Color.parseColor("#8B8B8B"));
                nfT.setTextColor(Color.parseColor("#8B8B8B"));
                if(i==1){
                    linT.setTextColor(Color.parseColor("#272727"));
                }else if(i==2){
                    nfT.setTextColor(Color.parseColor("#272727"));
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
}
